﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using VideoClipNameHelper;

namespace FindNoGain
{
    class Program
    {
        const string NgDir = ".NoRg";
        //
        private static bool onlyShow = false;
        private static int argsBase = 0;
        //
        static void Main(string[] args)
        {
            if (args.Length >= 1)
            {
                if (0 == String.Compare("-h", args[0], true) || 0 == String.Compare("-help", args[0], true) || "-?" == args[0])
                {
                    Usage();
                    return;
                }
                else if (0 == String.Compare("-onlyShow", args[0], true))
                {
                    onlyShow = true;
                }
                else
                {
                    Usage(); return;
                }
            }
            //
            string[] ff = Directory.GetFiles(".", "*.mkv", SearchOption.TopDirectoryOnly);
            //
            if (onlyShow)
            {
                foreach (string f in ff)
                {
                    double v;
                    if (!ReplayGain.ReadGain(f, out v))
                    {
                        Console.WriteLine($"{Path.GetFileName(f)}\t*** NO RG");
                    }
                    else
                    {
                        Console.WriteLine($"{Path.GetFileName(f)}\t{v:N1}");
                    }
                }
                return;
            }
            //
            if(!Directory.Exists(NgDir))
            {
                Directory.CreateDirectory(NgDir);
            }
            //
            int nAll = 0, nBad = 0;
            double rgMax = 0.0, rgMin = 0.0;
            string fnMax = "", fnMin = "";
            foreach(string f in ff)
            {
                nAll++;
                double v;
                if(!ReplayGain.ReadGain(f, out v))
                {
                    nBad++;
                    Console.WriteLine("{0}", Path.GetFileName(f));
                    //
                    Directory.Move(f, Path.Combine(NgDir, f));
                    //
                }
                else
                {
                    if (rgMax < v)
                    {
                        rgMax = v;
                        fnMax = Path.GetFileName(f);
                    }
                    if (v < rgMin)
                    {
                        rgMin = v;
                        fnMin = Path.GetFileName(f);
                    }
                }
            }
            //
            Console.WriteLine($"All={nAll}, WithoutRG={nBad}");
            Console.WriteLine($"RG Max={rgMax:N1} [{fnMax}]");
            Console.WriteLine($"RG Min={rgMin:N1} [{fnMin}]");
            //
        }
        //
        static void Usage()
        {
            Console.WriteLine("Usage:");
            Console.WriteLine("{0} [-onlyShow]", Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs()[0]));
            Console.WriteLine("-onlyShow - only show ReplayGain information");
            Console.WriteLine();
        }
        //
    }//clss
}//namespace
